package com.tz.service.impl;

import com.tz.entity.RightControl;
import com.tz.entity.Role;
import com.tz.mapper.RoleMapper;
import com.tz.service.RoleService;
import com.tz.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 本类拥有处理用户的权限
 *
 * @author Alex
 * @date 18-1-2
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;

    @Override
    public PageBean<Role> queryRolesByPage(Integer pageNow, Integer pageSize) {
        PageBean<Role> pb = new PageBean<>();
        pb.setPageDatas(roleMapper.selectRoleByPage(pageNow, pageSize));
        pb.setPageNow(pageNow);
        pb.setPageSize(pageSize);
        pb.setTotalRecords(roleMapper.countRole());
        return pb;
    }

    @Override
    public void removeBatchRoles(List<Long> ids) {
        for (Long id : ids) {
            roleMapper.deleteRoleRights(id);
        }
        roleMapper.deleteBatchRoles(ids);
    }

    @Override
    public void changeRight(Long id, List<Long> rightIds) {
        roleMapper.deleteRoleRights(id);
        if (rightIds != null && rightIds.size() != 0) {
            roleMapper.insertBatchRights(id, rightIds);
        }

    }


    @Override
    public void addRole(Role role) {
        roleMapper.insertRole(role);
    }

    @Override
    public Integer checkRoleName(String roleName) {
        return roleMapper.checkRoleName(roleName);
    }

    @Override
    public List<RightControl> getRole(Long roleId) {
        return roleMapper.selectRightControls(roleId);
    }

    @Override
    public List<RightControl> getAllRights() {
        return roleMapper.selectAllControls();
    }


    @Override
    public List<Role> getAllRoles() {
        return roleMapper.selectAllRole();
    }
}
